package igloo.model;

import igloo.util.DateUtil;
import igloo.util.EventSupport;
import igloo.util.IEventRaiser;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.amazonaws.services.dynamodb.model.AttributeValue;

public class Archive implements IEventRaiser<IArchiveListener>
{
    private final EventSupport<IArchiveListener> mEventSupport = new EventSupport<IArchiveListener>();

    private Vault mVault;

    private String mId;
    private String mName;
    private String mDescription;

    private long mSize;

    private Date mLocalUpdateDate;
    private Date mRemoteUpdateDate;

    private String mHostName;
    private String mPath;

    public Vault getVault()
    {
        return mVault;
    }

    public void setVault(Vault vault)
    {
        mVault = vault;
    }

    public String getId()
    {
        return mId;
    }

    public void setId(String id)
    {
        mId = id;
    }

    public String getName()
    {
        return mName;
    }

    public void setName(String name)
    {
        mName = name;
    }

    public String getDescription()
    {
        return mDescription;
    }

    public void setDescription(String desc)
    {
        mDescription = desc;
    }

    public long getSize()
    {
        return mSize;
    }

    public void setSize(long size)
    {
        mSize = size;
    }

    public Date getLocalUpdateDate()
    {
        return mLocalUpdateDate;
    }

    public void setLocalUpdateDate(Date d)
    {
        mLocalUpdateDate = d;
    }

    public Date getRemoteUpdateDate()
    {
        return mRemoteUpdateDate;
    }

    public void setRemoteUpdateDate(Date d)
    {
        mRemoteUpdateDate = d;
    }

    public String getVaultId()
    {
        final StringBuilder builder = new StringBuilder();
        builder.append(mVault.getRegion().name());
        builder.append('/');
        builder.append(mVault.getName());
        return builder.toString();
    }

    @Override
    public void addListener(IArchiveListener listener)
    {
        mEventSupport.addListener(listener);
    }

    @Override
    public void removeListener(IArchiveListener listener)
    {
        mEventSupport.removeListener(listener);
    }

    public String getHostName()
    {
        return mHostName;
    }

    public void setHostName(String hostName)
    {
        mHostName = hostName;
    }

    public String getPath()
    {
        return mPath;
    }

    public void setPath(String path)
    {
        mPath = path;
    }

    public Map<String, AttributeValue> toItem()
    {
        Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();

        // Required: DynamoDB hash key
        item.put("vaultId", new AttributeValue().withS(getVaultId()));

        // Required: DynamoDB range key
        item.put("id", new AttributeValue().withS(mId));

        String desc = (mDescription != null) ? mDescription : "";
        item.put("desc", new AttributeValue().withS(desc));

        String host = (mHostName != null) ? mHostName : "";
        item.put("host", new AttributeValue().withS(host));

        if(mLocalUpdateDate != null)
            item.put("localUpdate", new AttributeValue().withS(DateUtil.format(mLocalUpdateDate)));

        String name = (mName != null) ? mName : "";
        item.put("name", new AttributeValue().withS(name));

        String path = (mPath != null) ? mPath : "";
        item.put("path", new AttributeValue().withS(path));

        if(mRemoteUpdateDate != null)
            item.put("remoteUpdate", new AttributeValue().withS(DateUtil.format(mRemoteUpdateDate)));

        item.put("size", new AttributeValue().withN(String.valueOf(mSize)));

        String vault = (mVault != null) ? mVault.getName() : "";
        item.put("vault", new AttributeValue().withS(vault));

        return item;
    }
}
